﻿using Jizsoft.JFrame.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jizsoft.JFrame.Modeling.Domain.Services
{
    public class PublishService
    {
        public ServiceResult PublishModel(Guid id)
        {
            using(JFDbContext dbContext = new JFDbContext())
            {
                Entities.Model model = dbContext.Set<Entities.Model>().SingleOrDefault(p => p.Id == id && p.IsDeleted == false);
                if(model == null)
                {
                    return new ServiceResult(1, string.Empty);
                }

                model.PublishedTime = DateTime.Now;
                model.PublisherId = "admin";

                dbContext.Entry<Entities.Model>(model).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch(Exception ex)
                {
                    return new ServiceResult(1, ex.Message);
                }
            }
        }
    }
}
